package org.eweb4j.orm.jdbc.transaction;

import java.sql.SQLException;

import org.eweb4j.config.LogFactory;
import org.eweb4j.orm.dao.DAOException;
import org.eweb4j.util.StringUtil;


public class Transaction {
	public static void execute(Trans trans,Object... args) throws DAOException {
		try {
			trans.begin();
			trans.run(args);// 执行事务
			trans.commit();
		} catch (Exception e) {
			try {
				trans.rollback();// 回滚事务
			} catch (SQLException e1) {
				throw new DAOException("roll back exception ", e);
			}
			String info = StringUtil.getExceptionString(e);
			LogFactory.getORMLogger("info").write(info);
			throw new DAOException("transaction execute exception ", e);
		} finally {
			try {
				trans.close();
			} catch (SQLException e) {
				throw new DAOException("transaction close exception ", e);
			}
		}
	}
}
